package com.augurit.gzcgwsp.rqqyjyxk.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;

import com.augurit.gzcgwsp.rqqyjyxk.dao.YwRqQyjyxkSsylbDao;
import com.augurit.gzcgwsp.rqqyjyxk.entity.YwRqQyjyxkFinalRqssylb;
import com.augurit.gzcgwsp.rqqyjyxk.service.IYwRqQyjyxkService;
import com.augurit.gzcgwsp.rqqyjyxk.service.IYwRqQyjyxkSsylbService;
import com.augurit.gzcgwsp.rqqyjyxk.web.form.YwRqQyjyxkForm;
import com.augurit.gzcgwsp.rqqyjyxk.web.form.YwRqQyjyxkSsylbForm;
import com.augurit.gzcgwsp.ssylb.entity.YwRqRqssylb;

/**
 * 
 * @author Administrator
 *
 */
@Transactional
@Service
public class YwRqQyjyxkSsylbServiceImpl implements IYwRqQyjyxkSsylbService {
	@Autowired
	private YwRqQyjyxkSsylbDao ywRqQyjyxkSsylbDao;
	@Autowired
	private IYwRqQyjyxkService ywRqQyjyxkService;
	/*
	 * 分页查询
	 * @see com.augurit.gzcgwsp.rqqyjyxk.service.IYwRqQyjyxkSsylbService#getPage(java.lang.String)
	 */
	public Page<YwRqQyjyxkSsylbForm> getPage(Long qyjyxkid,Page<YwRqQyjyxkSsylbForm> page,String type) {
		String hql=" from YwRqRqssylb where qyjyxkid="+qyjyxkid+" order by ssylbid desc";
		if("2".equals(type)){//查询原来的数据
			YwRqQyjyxkForm form=this.ywRqQyjyxkService.get(qyjyxkid);
			 hql=" from YwRqQyjyxkFinalRqssylb where qyid="+form.getQyid()+" order by id desc";
			 Page pages=ywRqQyjyxkSsylbDao.findPage(page, hql, new HashMap());
			 List list=pages.getResult();
			 List<YwRqQyjyxkSsylbForm> datas=new ArrayList<YwRqQyjyxkSsylbForm>();
			 if(list!=null && list.size()>0){
				 for(int i=0;i<list.size();i++){
					 YwRqQyjyxkFinalRqssylb entity=(YwRqQyjyxkFinalRqssylb) list.get(i);
					 YwRqQyjyxkSsylbForm ssform = new DozerBeanMapper().map(entity, YwRqQyjyxkSsylbForm.class);
					 ssform.setSsaddress(entity.getDz());
					 ssform.setSsmc(entity.getMc());
					 ssform.setSsylbid(entity.getId());
					 datas.add(ssform);
				 }
			 }
			 pages.setResult(datas);
			 return pages;
		}
		 return ywRqQyjyxkSsylbDao.findPage(page, hql, new HashMap());
	}
	
	/*
	 * 保存
	 * @see com.augurit.gzcgwsp.rqqyjyxk.service.IYwRqQyjyxkSsylbService#saveEntity(java.util.Map)
	 */
	public boolean saveEntity(Map map) {
		String type=(String) map.get("type");
		String sslybid=(String) map.get("ssylbid");
		String qyjyxkid=(String)map.get("qyjyxkid");
		boolean flag=false;
		try {
			if("2".equals(type)){
				map.put("dz", map.get("ssaddress"));
				map.put("mc", map.get("ssmc"));
				map.put("id", sslybid);
				JSONObject jsonObject = JSONObject.fromObject(map); 
				YwRqQyjyxkFinalRqssylb entity=(YwRqQyjyxkFinalRqssylb) JSONObject.toBean(jsonObject, YwRqQyjyxkFinalRqssylb.class);
				ywRqQyjyxkSsylbDao.save(entity);
			}else{
				JSONObject jsonObject = JSONObject.fromObject(map); 
				YwRqRqssylb entity=(YwRqRqssylb) JSONObject.toBean(jsonObject, YwRqRqssylb.class);
				ywRqQyjyxkSsylbDao.save(entity);
			}
			flag=true;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}

	public YwRqQyjyxkSsylbForm getForm(String sslybid) {
		String sql=" from YwRqRqssylb where ssylbid="+sslybid;
		List<YwRqRqssylb> list=ywRqQyjyxkSsylbDao.find(sql);
		YwRqRqssylb entity=list.get(0);
		YwRqQyjyxkSsylbForm form = new DozerBeanMapper().map(entity, YwRqQyjyxkSsylbForm.class);
		return form;
	}

	
	public void doDelete(String id,String type) {
		String sql=" from YwRqRqssylb where ssylbid="+id;
		if("2".equals(type)){//删除原来的数据
			 sql=" from YwRqQyjyxkFinalRqssylb where id="+id;
			List<YwRqQyjyxkFinalRqssylb> list=ywRqQyjyxkSsylbDao.find(sql);
			YwRqQyjyxkFinalRqssylb entity=list.get(0);
			ywRqQyjyxkSsylbDao.doDelete(entity);
		}else{
			List<YwRqRqssylb> list=ywRqQyjyxkSsylbDao.find(sql);
			YwRqRqssylb entity=list.get(0);
			ywRqQyjyxkSsylbDao.doDelete(entity);
		}
	}

	public YwRqQyjyxkSsylbForm getFinalForm(String sslybid) {
		String sql=" from YwRqQyjyxkFinalRqssylb where id="+sslybid;
		List<YwRqQyjyxkFinalRqssylb> list=ywRqQyjyxkSsylbDao.find(sql);
		YwRqQyjyxkFinalRqssylb entity=list.get(0);
		YwRqQyjyxkSsylbForm form = new DozerBeanMapper().map(entity, YwRqQyjyxkSsylbForm.class);
		form.setSsaddress(entity.getDz());
		form.setSsmc(entity.getMc());
		form.setSsylbid(entity.getId());
		return form;
		
	}



}
